//
//  FHStatusPictrueView.swift
//  WeiBo
//
//  Created by qfh on 16/7/15.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

//  重用标记
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"
//  每项的间距
private let itemMargin: CGFloat = 5
//  每项的宽度
private let itemWidth: CGFloat = CGFloat(Int(ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin) / 3)


class FHStatusPictrueView: UICollectionView {
    
    lazy private var messageLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.blueColor(), size: 30)
        
        return label
    
    }()
    
    
    var pictures: [FHStatusPictureInfo]? {
    
        didSet {
        
            messageLabel.text = "\(pictures?.count ?? 0)"
            
            let itemSize = self.calcPictureViewSize(pictures?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(itemSize)
            }
            
            self.reloadData()
        
        }
    
    
    
    }

   
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let fayout = UICollectionViewFlowLayout()
        
        fayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        fayout.minimumInteritemSpacing = itemMargin
        
        fayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: fayout)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpUI() {
    
        delegate = self
        
        dataSource = self
        
        registerClass(FHStatusPictrueCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        
        addSubview(messageLabel)
        
        backgroundColor = UIColor.yellowColor()
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    
    
    }
    
    
    private func calcPictureViewSize(count: Int) -> CGSize {
    
        // 列数
        
        let cols = count > 3 ? 3 : count
        
        // 行数
        
        let  rows = (count - 1) / 3  + 1
        
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin

        let height = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        return CGSize(width: width, height: height)
    
    
    
    }
    

}



extension FHStatusPictrueView: UICollectionViewDelegate, UICollectionViewDataSource {


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! FHStatusPictrueCell
        
        cell.pictUrl = pictures![indexPath.item]
        
        return cell
        
    }



}


 /// 自定义cell

class FHStatusPictrueCell: UICollectionViewCell {

    var pictUrl: FHStatusPictureInfo? {
    
        didSet {
        
            if let imgUrl = pictUrl?.thumbnail_pic {
            
                imageView.sd_setImageWithURL(NSURL(string: imgUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
                
               gifImage.hidden = !imgUrl.hasPrefix(".gif")
                

                
            }
        
        
        }
    
    
    }
    
    
    lazy var imageView: UIImageView = {
    
        
        let  imgView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        imgView.contentMode = .ScaleAspectFit
        
        imgView.clipsToBounds = true
        
        
        return imgView
    
    }()
    
    lazy var gifImage: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
        
    }
    
    
    func setUpUI() {
    
    
        contentView.addSubview(imageView)
        
        contentView.addSubview(gifImage)
        
        imageView.snp_makeConstraints(closure: { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        })
    
        gifImage.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            
            make.bottom.equalTo(imageView)
        }
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



}




